<html>
<body>
Optimist is a lightweight framework to enable optimistic locking when
using MyBatis for Java.

<h3>Requirements</h3>
<ul>
<li>MyBatis for Java 3</li>
<li>MySQL 5.0 or 5.1</li>
<li>Entity classes using either Long or Integer as keys</li>
<li>During insert, update or delete the entity class needs to be the parameter to the SQL map</li>
</ul>

<h3>Getting started</h3>
<ol>
	<li>Add the following lines to your MyBatis configuration file.
	<pre>
   <span style="color: green;">&lt;plugins&gt;</span>
      <span style="color: green;">&lt;plugin</span> <span style="color: purple;">interceptor</span>=<span style="color: blue;">"com.spagettikod.optimist.OptimisticLockingInterceptor"</span><span style="color: green;">&gt;&lt;/plugin&gt;</span>
   <span style="color: green;">&lt;/plugins&gt;</span>
	</pre>
	</li>
	<li>Annotate your entity class for optimistic locking. Or make all your entity classes extend a
	super entity class to avoid adding id and version to all your entity classes.
	<pre>
   <span style="color: grey;">@OptimisticLocking</span>(<span style="color: blue;">"my_entity_table"</span>)
   <span style="color: purple;">public class</span> MyEntity {
   
      <span style="color: grey;">@Identity</span>(<span style="color: blue;">"id"</span>)
      <span style="color: purple;">private</span> Long id;
      
      <span style="color: purple;">private</span> String value;
      
      <span style="color: grey;">@Version</span>(<span style="color: blue;">"version"</span>)
      <span style="color: purple;">private</span> Long version;
      
   }
	</pre>
	</li>
	<li>Make sure the entity object is the parameter sent into the mapped statements
	<pre>
   <span style="color: green;">&lt;insert</span> <span style="color: purple;">id</span>=<span style="color: blue;">"insertEntity"</span> <span style="color: purple;">parameterType</span>=<span style="color: blue;">"MyEntity"</span> <span style="color: purple;">useGeneratedKeys</span>=<span style="color: blue;">"true"</span> <span style="color: purple;">keyProperty</span>=<span style="color: blue;">"id"</span><span style="color: green;">&gt;</span>
      INSERT INTO entity (
         value,
         version
      ) VALUES (
         #{value},
         #{version}
      )
   <span style="color: green;">&lt;/insert&gt;</span>
	
   <span style="color: green;">&lt;update</span> <span style="color: purple;">id</span>=<span style="color: blue;">"updateEntity"</span> <span style="color: purple;">parameterType</span>=<span style="color: blue;">"MyEntity"</span><span style="color: green;">&gt;</span>
      UPDATE entity SET
         value = #{value},
         version = #{version}
      WHERE id = #{id}
   <span style="color: green;">&lt;/update&gt;</span>
	
   <span style="color: green;">&lt;delete</span> <span style="color: purple;">id</span>=<span style="color: blue;">"deleteEntity"</span> <span style="color: purple;">parameterType</span>=<span style="color: blue;">"MyEntity"</span> <span style="color: green;">&gt;</span>
      DELETE FROM entity WHERE id = #{id}
   <span style="color: green;">&lt;/delete&gt;</span>
	</pre>
	</li>
</ol>
</body>
</html>